package com.luhai.shipmentscheck.util;

import android.os.Handler;
import android.os.Looper;

import com.luhai.shipmentscheck.application.MyApplication;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by jzh-创世头盔之父.
 * Date: 2019\10\22 0022* Time: 18:07
 * Emaile: 1186832904@qq.com
 * Introduction: jzh de code
 */
public class OkHttpUtils {


    private final Handler mHandler;
    private OkHttpClient mOkHttpClient;
    //声明类的变量
    private static volatile OkHttpUtils sOkHttpUtils;

    //私有构造
    private OkHttpUtils() {
        mHandler = new Handler(Looper.getMainLooper());
        File sdCache = MyApplication.getContext().getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder().
                connectTimeout(5, TimeUnit.SECONDS).
                writeTimeout(8, TimeUnit.SECONDS).
                readTimeout(8, TimeUnit.SECONDS).
                cache(new Cache(sdCache.getAbsoluteFile(), cacheSize));
        mOkHttpClient = okHttpClientBuilder.build();
    }

    public void setTimeOut(int connectedSeconds, int writeSeconds, int readSeconds) {
        File sdCache = MyApplication.getContext().getExternalCacheDir();
        int cacheSize = 10 * 1024 * 1024;
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder().
                connectTimeout(connectedSeconds, TimeUnit.SECONDS).
                writeTimeout(writeSeconds, TimeUnit.SECONDS).
                readTimeout(readSeconds, TimeUnit.SECONDS).
                cache(new Cache(sdCache.getAbsoluteFile(), cacheSize));
        mOkHttpClient = okHttpClientBuilder.build();
    }

    //暴露外界访问的方法,双层锁
    public static OkHttpUtils getInstance() {
        if (sOkHttpUtils == null) {
            synchronized (OkHttpUtils.class) {
                if (sOkHttpUtils == null) {
                    //创建对象
                    sOkHttpUtils = new OkHttpUtils();
                }
            }
        }
        //如果不等于空直接返回对象,符合单例的概念
        return sOkHttpUtils;
    }

    //创建接口
    public interface OkHttpCallBackLinener {
        void failure(Exception e);

        void success(String json);
    }

    //封装get参数定义两个,一个是URL网址   一个实现接口的对象
    public void doGet(String url, final OkHttpCallBackLinener callBack) {

        Request.Builder requestBuilder = new Request.Builder().
                url(url).
                method("GET", null);
        Request request = requestBuilder.build();

        Call call = mOkHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //切换到主线程
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (callBack != null) {
                            callBack.failure(e);
                        }
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String json = response.body().string();
                if (response != null) {
                    //切换到主线程
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (callBack != null) {
                                callBack.success(json);
                            }
                        }
                    });
                }
            }
        });

    }

}
